<?php


namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\System\Ad;
use App\Models\Semver;
use App\Models\Config\VersionUpdate;
use Illuminate\Http\Request;
use App\Models\Config;

class ConfigController extends Controller
{

    public function info(Request $request){

        // 修正下载
        $app_id  = $request->app_id ?? 0;
        $fixver = $request->input('fixver',0);

        if(10001 == $app_id && intval($fixver) < 3  ) {
            return $this->ajaxError('你當前 app 的版本太低，請刪除後重新下載~\n下載地址為: https://img-kuangchi.youfangkuangye.com');
        }

        $coins = C('coins', []);

        $min_withdrawal = C('min_withdrawal_'.strtolower($request->coin),0);
        //首页数据配置
        $people_num = Config::where('key','people_num')->value('value');
        $day_num = round((strtotime(date('Y-m-d'))-strtotime(Config::where('key','day_num')->value('value')))/3600/24);

        $customer_phone=C('customer_phone');
        $customer_wx=C('customer_wx');
        $network = [
            'out_fee_rate' => C('network_out_fee_rate_'.strtolower($request->coin)),
            'out_fee' => C('network_out_fee_'.strtolower($request->coin))
        ];
        $withdraw_coins = C('withdraw_coins', []);
        $data = [
            'coins' => $coins,
            'customer_phone' => $customer_phone,
            'customer_wx' => $customer_wx,
            'day_num' => $day_num,
            'people_num' => $people_num,
            'min_withdrawal' => $min_withdrawal,
            'network' => $network,
            'withdraw_coins' => $withdraw_coins
        ];
        return $this->ajaxSuccess($data);

    }


    public function package(Request $request)
    {
        $type = $request->input('ostype',0);
        $only=$request->only;
        $where = [];
        if(empty($type)){
            $where[] = ['package','=','wgt'];
        }else{
            if($only == 1){
                $where[] = ['package','=','apk'];
            }
        }

        $update= VersionUpdate::where($where)->orderBy('id','DESC')->first();

        $version = [
            't' => $update->title,
            'v' => $update->version,
            'vv' => $update->version,
            'u' => $update->download_url,
            'c' => $update->content,
            'f' => 1
        ];
        if($version) {
            return  $this->ajaxSuccess($version);
        }else{
            return $this->ajaxError();
        }
    }


    public function advertisement(Request $request){

        $d=Ad::where("status",1)->orderBy('updated_at','desc')->first();
        if($d){
            return $this->ajaxSuccess($d);
        }else{
            return $this->ajaxSuccess([]);
        }

    }

}
